﻿$.validator.unobtrusive.adapters.add('relationshipwith', ['othervalue', 'otherproperty', 'relationshiptype', 'propertyvaluetype'], function (options) {
    options.rules['relationshipwith'] = options.params;
    options.messages['relationshipwith'] = options.message;
});

$.validator.addMethod("relationshipwith", function (value, element, params) {
    var value2 = null;
    var property2 = null;
    var testValue = null;
    if (params.othervalue != null && params.othervalue != '') {
        value2 = params.othervalue;
    }
    if (params.otherproperty != null && params.otherproperty != '') {
        property2 = params.otherproperty;
    }
    if (value2 != null) {
        testValue = value2;
    } else {
        testValue = $('input[name="' + property2 + '"]').val();
    }
    if (params.propertyvaluetype == "DateTime") {
        if (isNaN(Date.parse(value)) || isNaN(Date.parse(testValue))) {
            return true;
        }
        if (params.relationshiptype == "GreaterThan") {
            return (Date.parse(value) > Date.parse(testValue));
        }
        else if (params.relationshiptype == "GreaterThanOrEqual") {
            return (Date.parse(value) >= Date.parse(testValue));
        }
        else if (params.relationshiptype == "LessThan") {
            return (Date.parse(value) < Date.parse(testValue));
        }
        else if (params.relationshiptype == "LessThanOrEqual") {
            return (Date.parse(value) <= Date.parse(testValue));
        }

    }
    else {
        if (value == "" || value == undefined || value == null
        || testValue == "" || testValue == undefined || testValue == null) {
            return true;
        }
        if (params.relationshiptype == "GreaterThan") {
            return (parseInt(value, 10) > parseInt(testValue, 10));
        }
        else if (params.relationshiptype == "GreaterThanOrEqual") {
            return (parseInt(value, 10) >= parseInt(testValue, 10));
        }
        else if (params.relationshiptype == "LessThan") {
            return (parseInt(value, 10) < parseInt(testValue, 10));
        }
        else if (params.relationshiptype == "LessThanOrEqual") {
            return (parseInt(value, 10) <= parseInt(testValue, 10));
        }
    }
    return true;
}, '');

//check datetime - dongth
$.validator.unobtrusive.adapters.add("datecms", [], function (options) {
    options.rules['datecms'] = options.params;
    options.messages["datecms"] = options.message;
});

$.validator.addMethod("datecms", function (value, element, params) {

    if (value == '') return true;
    try {
        var filter = /^(?:^(?:(?:(?:(?:(?:0?[13578]|1[02])\/31)|(?:(?:0?[13-9]|1[0-2])\/(?:29|30)))\/(?:1[6-9]|[2-9][0-9])[0-9]{2})|(?:0?2\/29\/(?:(?:(?:1[6-9]|[2-9][0-9])(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))\/(?:0?[1-9]|1[0-9]|2[0-8])\/(?:(?:1[6-9]|[2-9][0-9])[0-9]{2}))$)$/;
        var isValid = filter.test(value);
        return isValid;
    } catch (err) {
        return false;
    }
});

$.validator.unobtrusive.adapters.add("greaterthanzero", [], function (options) {
    options.rules['greaterthanzero'] = options.params;
    options.messages["greaterthanzero"] = options.message;
});

$.validator.addMethod("equalgreaterthanzero", function (value, element, params) {
    if (value < 0) return false;
    return true;
});

$.validator.unobtrusive.adapters.add("sqlinjectionprevention", [], function (options) {
    options.rules['sqlinjectionprevention'] = options.params;
    options.messages["sqlinjectionprevention"] = options.message;
});

$.validator.addMethod("sqlinjectionprevention", function (value, element, params) {
    var str = value.toString();
    for (var i = 0; i < str.length; i++) {
        switch (str[i]) {
            case ';':
                return false;
            case '-':
                if ((i + 1 < str.length) && (str[i + 1] == '-')) return false;
                break;
            case '/':
                if ((i + 1 < str.length) && (str[i + 1] == '*')) return false;
                break;
            case '*':
                if ((i + 1 < str.length) && (str[i + 1] == '/')) return false;
                break;
            case 'x':
                if ((i + 2 < str.length) && (str[i + 1] == 'p') && (str[i + 2] == '_')) return false;
                break;
            default:
                break;
        }
    }
    return true;
});

$.validator.unobtrusive.adapters.add("integercms", [], function (options) {
    options.rules['integercms'] = options.params;
    options.messages["integercms"] = options.message;
});

$.validator.addMethod("integercms", function (value, element, params) {
    value = $.trim(value);
    if (value == '') return true;
    var intRegex = /^\+?(0|[1-9]\d*)$/;
    return intRegex.test(value);
});

$.validator.unobtrusive.adapters.add("numbercms", [], function (options) {
    options.rules['numbercms'] = options.params;
    options.messages["numbercms"] = options.message;
});

$.validator.addMethod("numbercms", function (value, element, params) {
    value = $.trim(value);
    if (value == '') return true;
    var intRegex = /^\d+\.?\d*$/;
    return intRegex.test(value);
});

